#include "gSlider.h"

gSlider::gSlider()
{
	m_perc = 0.5f;
	m_drawInfo = false;
}

gSlider::~gSlider()
{

}

void gSlider::initSlider(
	char* a_bar,
	char* a_btn,
	char* a_icn,
	char* a_inf,
	V2DF  a_pos,
	V2DF  a_iconOff,
	V2DF  a_infoOff)
{
	m_bar.init(FRAMEWORKS_MEM_REG);
	m_bar.initTexture(a_bar,TEX_C,a_pos,V2D_ONES,0);

	m_btn.init(FRAMEWORKS_MEM_REG);
	m_btn.initTexture(a_btn,TEX_C,a_pos,V2D_ONES,0);

	m_icn.init(FRAMEWORKS_MEM_REG);
	m_icn.initTexture(a_icn,TEX_C,a_pos+a_iconOff,V2D_ONES,0);

	m_inf.init(FRAMEWORKS_MEM_REG);
	m_inf.initTexture(a_inf,TEX_C,a_pos+a_infoOff,V2D_ONES,0);

	function<void()> idle = [&]()
	{
		if(!m_bar.isHover() && !m_icn.isHover())
		{
			m_drawInfo=false;
			m_btn.setScl(V2DF(1.0f,1.0f));
			m_icn.setScl(V2DF(1.0f,1.0f));
		}
	};

	function<void()> hover = [&]()
	{
		if(m_bar.isHover() || m_icn.isHover())
		{
			m_drawInfo=true;
			m_btn.setScl(V2DF(1.08f,1.08f));
			m_icn.setScl(V2DF(1.08f,1.08f));
		}
	};

	m_bar.onHeld([&]()
	{
		float y = 0;
		m_din->mousePosition(0,&y);
		m_btn.refTrn().y = y;

		m_perc = abs((m_btn.getTrn().y-m_bar.getMax().y))/m_bar.getHeight();
	});

	m_bar.onIdle(idle);
	m_bar.onHover(hover);
	m_icn.onIdle(idle);
	m_icn.onHover(hover);
}

void gSlider::input()
{
	m_bar.input();
	m_btn.input();
	m_icn.input();
}

void gSlider::update()
{
	m_bar.update();
	m_btn.update();
	m_icn.update();
	m_inf.update();
}

void gSlider::draw()
{
	m_bar.draw();
	m_btn.draw();
	m_icn.draw();

	if(m_drawInfo) m_inf.draw();
}

void gSlider::setPerc(float a_perc)
{
	m_perc = a_perc;
	m_btn.refTrn().y = m_bar.getMax().y-(m_bar.getHeight() * m_perc);
}